class Settings {
    constructor(root) {
        this.root = root;
        //表明在哪个端的参数
        this.platform = "WEB";
        if (this.root.AcWingOS) this.platform = "ACAPP";
        //接收后端参数
        this.username = "";
        this.photo = "";

        this.$settings = $(`
            <div class="ac-game-settings">
    <div class="ac-game-settings-login">
        <div class="ac-game-settings-title">
            登录
        </div>
        <div class="ac-game-settings-username">
            <div class="ac-game-settings-item">
                <input type="text" placeholder="用户名">
            </div>
        </div>
        <div class="ac-game-settings-password">
            <div class="ac-game-settings-item">
                <input type="password" placeholder="密码">
            </div>
        </div>
        <div class="ac-game-settings-submit">
            <div class="ac-game-settings-item">
                <input type="button" value="登录">
            </div>
        </div>
        <div class="ac-game-settings-error-message">
        </div>
        <div class="ac-game-settings-option">
            注册
        </div>
        <br>
        <div class="ac-game-settings-quick-login">
            <div class="ac-game-settings-quick-login-acwing">
                <img width="30" height="30" src="https://app165.acapp.acwing.com.cn/static/image/settings/acwing_logo.png">
                <br>
                <div>
                    Acwing
                </div>
            </div>
             <div class="ac-game-settings-quick-login-gitee">
                <img width="30" height="30" src="https://cdn.acwing.com/media/article/image/2022/03/27/82975_fe4ed5d3ad-gitee.png" >
                <br>
                <div>
                    gitee
                </div>
            </div>
            <div class="ac-game-settings-quick-login-github">
                <img width="30" src="https://cdn.acwing.com/media/article/image/2022/03/27/82975_055d5bf1ad-github.png" >
                <br>
                <div>
                    github
                </div>
            </div>
        </div>
    </div>
    <div class="ac-game-settings-register">
        <div class="ac-game-settings-title">
            注册
        </div>
        <div class="ac-game-settings-username">
            <div class="ac-game-settings-item">
                <input type="text" placeholder="用户名">
            </div>
        </div>
        <div class="ac-game-settings-password ac-game-settings-password-first">
            <div class="ac-game-settings-item">
                <input type="password" placeholder="密码">
            </div>
        </div>
        <div class="ac-game-settings-password ac-game-settings-password-second">
            <div class="ac-game-settings-item">
                <input type="password" placeholder="确认密码">
            </div>
        </div>
        <div class="ac-game-settings-submit">
            <div class="ac-game-settings-item">
                <input type="button" value="注册">
            </div>
        </div>
        <div class="ac-game-settings-error-message">
        </div>
        <div class="ac-game-settings-option">
            登录
        </div>
        <br>
        <div class="ac-game-settings-quick-login">
            <div class="ac-game-settings-quick-login-acwing">
                <img width="30" src="https://app165.acapp.acwing.com.cn/static/image/settings/acwing_logo.png" >
                <div>
                    AcWing
                </div>
            </div>
            <div class="ac-game-settings-quick-login-gitee">
                <img width="30" src="https://cdn.acwing.com/media/article/image/2022/03/27/82975_fe4ed5d3ad-gitee.png" >
                <div>
                    gitee
                </div>
            </div>
            <div class="ac-game-settings-quick-login-github">
                <img width="30" src="https://cdn.acwing.com/media/article/image/2022/03/27/82975_055d5bf1ad-github.png" >
                <div>
                    github
                </div>
            </div>
            
        </div>
    </div>
</div>
        `);
        //把登录界面加载到DOM树中
        this.root.$ac_game.append(this.$settings);
        //找到登录窗口节点及其子树
        this.$login = this.$settings.find(".ac-game-settings-login");
        //找到登录窗口的输入用户名表单
        this.$login_username = this.$login.find(".ac-game-settings-username input");
        //找到登录窗口的输入密码表单
        this.$login_password = this.$login.find(".ac-game-settings-password input");
        //找到登录窗口的登录按钮
        this.$login_submit = this.$login.find(".ac-game-settings-submit>div>input");
        //显示错误信息
        this.$login_error_message = this.$login.find(".ac-game-settings-error-message");
        ////找到登录窗口的注册按钮
        this.$login_register = this.$login.find(".ac-game-settings-option");

        this.$login.hide();

        this.$register = this.$settings.find(".ac-game-settings-register");
        this.$register_username = this.$register.find(".ac-game-settings-username input");
        this.$register_password = this.$register.find(".ac-game-settings-password-first input");
        this.$register_password_confirm = this.$register.find(".ac-game-settings-password-second input");
        this.$register_submit = this.$register.find(".ac-game-settings-submit>div>input");
        this.$register_error_message = this.$register.find(".ac-game-settings-error-message");
        this.$register_login = this.$register.find(".ac-game-settings-option");
        this.$acwing_login = this.$settings.find('.ac-game-settings-quick-login-acwing img');
        this.$gitee_login = this.$settings.find('.ac-game-settings-quick-login-gitee img');
        this.$github_login = this.$settings.find('.ac-game-settings-quick-login-github img');
        this.$register.hide();
        console.log(this.$github_login)
        this.root.$ac_game.append(this.$settings);

        this.start();
    }

    /**
     * 在对象创建时执行的函数
     */
    start() {
        if (this.platform === "ACAPP") {
            this.getinfo_acapp();
        } else {
            if (this.root.access) {
                this.getinfo_web();
                this.refresh_jwt_token();

            } else {
                this.open_login_view();
            }
            this.add_listening_events();
        }

    }

    /**
     * 获得后端信息的函数
     */
    getinfo_web() {
        // console.log(6666)
        $.ajax({
            url: "https://game.liuyutao666.top/settings/getinfo/",
            type: "GET",
            data: {
                platform: this.platform,
            },
            headers: {
                //这里的Bearer是settings中，SIMPLE_JWT，中定义的'AUTH_HEADER_TYPES': ('Bearer',),
                //注意这里Bearer后必须有空格
                'Authorization': "Bearer " + this.root.access,
            },
            //得到后端参数后执行的函数
            success: resp => {
                if (resp.result === "success") {
                    this.username = resp.username;
                    this.photo = resp.photo;
                    //登录界面隐藏
                    this.hide();
                    //显示菜单界面
                    this.root.menu.show();
                } else {
                    //获取信息失败(即用户未登录)，则继续显示登录界面
                    this.open_login_view();
                }
            }
        });
    }

    /**
     * 打开登录界面
     */
    open_login_view() {
        this.$register.hide();
        this.$login.show();
    }

    /**
     * 打开注册界面
     */
    open_register() {
        this.$login.hide();
        this.$register.show();
    }

    /**
     * 隐藏注册/登录界面
     */
    hide() {
        this.$settings.hide();
    }

    /**
     * 显示注册/登录界面
     */
    show() {
        this.$settings.show();
    }

    /**
     * 鼠标点击后触发的函数
     */
    add_listening_events() {
        let outer = this;
        this.add_listening_events_login();
        this.add_listening_events_register();

        this.$acwing_login.click(function () {
            outer.acwing_login();
        });
        this.$gitee_login.click(function () {
            outer.gitee_login();
        });
        this.$github_login.click(function () {
            outer.github_login();
        });


    }

    /**
     *
     */
    acwing_login() {
        $.ajax({
            url: "https://game.liuyutao666.top/settings/acwing_info/web/apply_code/",
            type: "GET",
            success: function (resp) {
                console.log(resp);
                if (resp.result === "success") {
                    window.location.replace(resp.apply_code_url);
                }
            }
        });
    }

    gitee_login() {
        $.ajax({
            url: "https://game.liuyutao666.top/settings/gitee_info/apply_code/",
            type: "GET",
            success: function (resp) {
                console.log(resp);
                if (resp.result === "success") {
                    window.location.replace(resp.apply_code_url);
                }
            }
        });
    }

    github_login() {
        $.ajax({
            url: "https://game.liuyutao666.top/settings/github_info/apply_code/",
            type: "GET",
            success: function (resp) {
                console.log(resp);
                if (resp.result === "success") {
                    window.location.replace(resp.apply_code_url);
                }
            }
        });
    }

    acapp_login(appid, redirect_uri, scope, state) {
        let outer = this;

        this.root.AcWingOS.api.oauth2.authorize(appid, redirect_uri, scope, state, function (resp) {
            console.log("called from acapp_login function");
            console.log(resp);
            if (resp.result === "success") {
                outer.username = resp.username;
                outer.photo = resp.photo;
                outer.hide();
                outer.root.menu.show();
            }
        });
    }

    /**
     * acapp端一键登录
     */
    getinfo_acapp() {
        let outer = this;

        $.ajax({
            url: "https://game.liuyutao666.top/settings/acwing_info/acapp/apply_code/",
            type: "GET",
            success: function (resp) {
                if (resp.result === "success") {
                    outer.acapp_login(resp.appid, resp.redirect_uri, resp.scope, resp.state);
                }
            }
        });
    }


    /**
     * 点击登录按钮后触发的函数
     */
    add_listening_events_login() {
        let outer = this;

        this.$login_register.click(function () {
            outer.open_register();
        });
        this.$login_submit.click(function () {
            outer.login_on_remote();
        });
    }

    /**
     * 点击注册按钮后触发的函数
     */
    add_listening_events_register() {
        let outer = this;
        this.$register_login.click(function () {
            // console.log('666')
            outer.open_login_view();
        });
        this.$register_submit.click(function () {
            outer.register_on_remote();
        });
    }

    /**
     * 向后端发送填写的账号密码，验证并返回信息
     */
    login_on_remote() {  // 在远程服务器上登录
        let username = this.$login_username.val();
        let password = this.$login_password.val();
        this.$login_error_message.empty();

        $.ajax({
            url: "https://game.liuyutao666.top/settings/token/",
            type: "post",
            data: {
                username: username,
                password: password,
            },
            success: resp => {
                console.log(resp);
                this.root.access = resp.access;
                this.root.refresh = resp.refresh;
                this.refresh_jwt_token();
                this.getinfo_web();
            },

            error: () => {
                this.$login_error_message.html("用户名或密码错误");
            }

        })
        ;
    }

    /**
     * 处理JWT刷新
     */

    refresh_jwt_token() {
        setInterval(() => {
            $.ajax({
                url: "https://game.liuyutao666.top/settings/token/refresh/",
                type: "post",
                data: {
                    refresh: this.root.refresh,
                },
                success: resp => {
                    this.root.access = resp.access;
                }
            });
        }, 4.5 * 60 * 1000);

        setTimeout(() => {
            $.ajax({
                url: "https://game.liuyutao666.top/settings/ranklist/",
                type: "get",
                headers: {
                    'Authorization': "Bearer " + this.root.access,
                },
                success: resp => {
                    console.log(resp);
                }
            });
        }, 5000);

    }

    /**
     * 注册用户
     */
    register_on_remote() {  // 在远程服务器上注册
        let outer = this;
        let username = this.$register_username.val();
        let password = this.$register_password.val();
        let password_confirm = this.$register_password_confirm.val();
        this.$register_error_message.empty();

        $.ajax({
            url: "https://game.liuyutao666.top/settings/register/",
            type: "post",
            data: {
                username: username,
                password: password,
                password_confirm: password_confirm,
            },
            success: function (resp) {
                console.log(resp);
                if (resp.result === "success") {
                    location.reload();  // 刷新页面
                } else {
                    outer.$register_error_message.html(resp.result);
                }
            }
        });
    }

    /**
     * 实现用户退出登录
     * @returns {boolean}
     */
    logout_on_remote() {
        if (this.platform === "ACAPP") {
            this.root.AcWingOS.api.window.close();
        } else {
            this.root.access = "";
            this.root.refresh = "";
            location.href = "/"
        }

        $.ajax({
            url: "https://game.liuyutao666.top/settings/logout/",
            type: "GET",
            success: function (resp) {
                console.log(resp);
                if (resp.result === "success") {
                    //刷新界面并清空cookie
                    location.reload();
                }
            }
        });
    }
}